package com.newly.center.sys.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.newly.api.sys.feign.AuthEntityClient;
import com.newly.center.sys.service.authEntity.api.IAuthEntityService;
import com.newly.common.base.constants.AuthConstant;
import com.newly.common.base.constants.ServerUriConstant;
import com.newly.common.base.entity.R;
import com.newly.common.base.entity.sys.auth.entity.bo.AuthEntityBo;
import com.newly.common.base.entity.sys.auth.entity.bo.AuthEntityTypeBo;
import com.newly.common.base.entity.sys.auth.entity.vo.AuthEntityTypeVo;
import com.newly.common.base.entity.sys.auth.entity.vo.AuthEntityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @program: newly-parent
 * @remark: 权限实体
 * @author: guan
 * @create: 2022-01-13 18:16
 */
@RestController
@RequestMapping(ServerUriConstant.NEWLY_SYS_AUTH_ENTITY)
public class AuthEntityApi implements AuthEntityClient {

    @Autowired
    private IAuthEntityService authEntityService;

    @Override
    public R queryTypes() {
        List<AuthEntityTypeVo> authEntityTypeVos = null;
        try {
            List<AuthEntityTypeBo> authEntityTypeBos = authEntityService.getTypes();
            if (!ObjectUtil.isEmpty(authEntityTypeBos)) {
                authEntityTypeVos = BeanUtil.copyToList(authEntityTypeBos, AuthEntityTypeVo.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(authEntityTypeVos);
    }

    @Override
    public R queryByTypeCode(Integer typeCode) {
        List<AuthEntityVo> authEntityVos = null;
        try {
            List<AuthEntityBo> authEntityBos = authEntityService.getByTypeCode(typeCode);
            if (!ObjectUtil.isEmpty(authEntityBos)) {
                authEntityVos = BeanUtil.copyToList(authEntityBos, AuthEntityVo.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(authEntityVos);
    }

    @Override
    public R listAuthEntity() {
        List<AuthEntityBo> authEntityBos = null;
        try {
            authEntityBos = authEntityService.listAuthEntity();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok(authEntityBos);
    }

    @Override
    public R queryIdsByRoleIdAndTypeCode(Integer roleId, Integer typeCode) {
        List<String> entityIds = null;
        try {
            entityIds = authEntityService.getIdsByRoleIdAndTypeCode(roleId, typeCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok(entityIds);
    }

    @Override
    public R queryAllIdsByRoleId(Integer roleId) {
        Map<Integer, List<String>> entityIdsMap = null;
        try {
            entityIdsMap = authEntityService.getAllIdsByRoleId(roleId);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(entityIdsMap);
    }

    @Override
    public R bind(AuthConstant.AuthEntityType entityType, List<String> entityIds) {
        boolean result = false;
        try {
            result = authEntityService.bind(entityType, entityIds);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(result);
    }

    @Override
    public R unbind(AuthConstant.AuthEntityType entityType, List<String> entityIds) {
        boolean result = false;
        try {
            result = authEntityService.unBind(entityType, entityIds);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(result);
    }
}
